package org.sys.transporttrainsys.controller;

import org.sys.transporttrainsys.entity.Organization;
import org.sys.transporttrainsys.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping("/api/organizations")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    @GetMapping
    public List<Organization> getAllOrganizations() {
        return organizationService.getAllOrganizations();
    }

    @GetMapping("/{id}")
    public ResponseEntity<Organization> getOrganizationById(@PathVariable Long id) {
        Organization organization = organizationService.getOrganizationById(id);
        return ResponseEntity.ok(organization);
    }

    @PostMapping
    public Organization createOrganization(@RequestBody Organization organization) {
        return organizationService.createOrganization(organization);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Organization> updateOrganization(@PathVariable Long id, @RequestBody Organization organization) {
        Organization updatedOrganization = organizationService.updateOrganization(id, organization);
        return ResponseEntity.ok(updatedOrganization);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteOrganization(@PathVariable Long id) {
        organizationService.deleteOrganization(id);
        return ResponseEntity.noContent().build();
    }

    // 根据名称查询
    @GetMapping("/search/name")
    public List<Organization> searchByName(@RequestParam String name) {
        return organizationService.findOrganizationsByName(name);
    }

    // 根据类型查询
    @GetMapping("/search/type")
    public List<Organization> searchByType(@RequestParam String type) {
        return organizationService.findOrganizationsByType(type);
    }

    // 根据名称和类型联合查询
    @GetMapping("/search")
    public List<Organization> searchByNameAndType(@RequestParam String name, @RequestParam String type) {
        return organizationService.findOrganizationsByNameAndType(name, type);
    }
}